﻿$.extend({

    UserManagementDA: {

        GetAllUsers: function () {

            var arryUsers = [];

            $().SPServices({
                operation: "GetUserCollectionFromSite",
                async: false,
                completefunc: function (xData) {
                    var liOptions = "";
                    $(xData.responseXML).find('User').each(function () {
                        var o = $(this);
                        var item = { UserName: o.attr("Name"), LoginName: o.attr('LoginName'), Email: o.attr('Email') };
                        arryUsers.push(item);
                    });
                }
            });

            return arryUsers;
        },

        GetAllUsersByGroup: function (groupName, siteUrl) {

            var arryUsers = [];
            var encodedName = $.UMUtilities.EncodeText(groupName);

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            $().SPServices({
                webURL: siteUrl,
                operation: "GetUserCollectionFromGroup",
                async: false,
                groupName: encodedName,
                completefunc: function (xData) {
                    var liOptions = "";
                    $(xData.responseXML).find('User').each(function () {
                        var o = $(this);
                        var item = { UserName: o.attr("Name"), LoginName: o.attr('LoginName'), Email: o.attr('Email') };
                        arryUsers.push(item);
                    });
                }
            });

            return arryUsers;
        },

        GetUserInfo: function (loginName) {

            var arryUserInfo = [];

            $().SPServices({
                operation: "GetUserInfo",
                async: false,
                userLoginName: loginName,
                completefunc: function (xData) {
                    $(xData.responseXML).find('User').each(function () {
                        var item = { UserId: $(this).attr("ID"), UserName: $(this).attr("Name") };
                        arryUserInfo.push(item);
                    });
                }
            });

            return arryUserInfo;
        },

        GetAllListItems: function (listName) {

            var arryUsers = [];

            $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: listName,
                CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='User' /></ViewFields>",
                completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFilterNode("z:row").each(function () {
                        var loginName = $(this).attr("ows_User").split('#');
                        var item = { UserName: $(this).attr("ows_Title"), LoginName: loginName[1] };
                        arryUsers.push(item);
                    });
                }
            });

            return arryUsers;
        },
        /*
        GetListItemsByUser: function (userName) {

            var arryGroups = [];
            var queryField =
                "<Query>" +
                    "<Where>" +
                        "<Eq>" +
                        "<FieldRef Name='Title' /><Value Type='Text'>" + userName + "</Value>" +
                        "</Eq>" +
                    "</Where>" +
                "</Query>";

            $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: "UserList",
                CAMLQuery: queryField,
                CAMLRowLimit: 100,
                CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='User' /><FieldRef Name='Group' /></ViewFields>",
                completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFilterNode("z:row").each(function () {
                        var groupName = $(this).attr("ows_Group").split('#');
                        var item = { Group: groupName[1] };
                        arryGroups.push(groupName[1]);
                    });
                }
            });

            return arryGroups.sort();
        },
        */
        GetGroupsByUser: function (userName, siteUrl) {

            var arryGroups = [];

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: "GetGroupCollectionFromUser",
                    async: false,
                    userLoginName: userName,
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("Group").each(function () {
                            var encodedName = $.UMUtilities.EncodeText($(this).attr("Name"));
                            var item = { GroupName: encodedName, Description: $(this).attr("Description") };
                            arryGroups.push(item);
                        });
                    }
                });
            }
            catch (e) {
                alert(e);
            }

            return arryGroups;
        },

        GetAllGroups: function (siteUrl) {

            var arryGroups = [];

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: "GetGroupCollectionFromWeb",
                    async: false,
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("Group").each(function () {
                            var encodedName = $.UMUtilities.EncodeText($(this).attr("Name"));
                            var item = { GroupName: encodedName, Description: $(this).attr("Description") };
                            arryGroups.push(item);
                        });
                    }
                });

                var arryUniqueGroups = arryGroups.sort($.UMUtilities.DynamicSort("GroupName"));
                return arryUniqueGroups;
            }
            catch (e) {
                alert(e);
            }
        },

        GetGroupInfo: function (groupName, siteUrl) {

            var arryGroups = [];
            var encodedName = groupName.replace('&', '&amp;');

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: "GetGroupInfo",
                    async: false,
                    groupName: encodedName,
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("Group").each(function () {
                            var item = { GroupName: $.UMUtilities.EncodeText($(this).attr("Name")), Description: $(this).attr("Description"), GroupID: $(this).attr("ID") };
                            arryGroups.push(item);
                        });
                    }
                });
            }
            catch (e) {
                alert(e);
            }

            return arryGroups;
        },

        AddUserToGroup: function (groupName, loginName, siteUrl) {

            var encodedName = $.UMUtilities.EncodeText(groupName);

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: "AddUserToGroup",
                    groupName: encodedName,
                    userLoginName: loginName,
                    async: false,
                    completefunc: null
                });
            }
            catch (e) {
                alert(e);
            }
        },

        RemoveUserFromGroup: function (groupName, loginName, siteUrl) {

            var encodedName = $.UMUtilities.EncodeText(groupName);

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: "RemoveUserFromGroup",
                    groupName: encodedName,
                    userLoginName: loginName,
                    async: false,
                    completefunc: null
                });
            }
            catch (e) {
                alert(e);
            }
        },

        CreateGroup: function (groupName, groupOwner, descr, oType, siteUrl) {

            var encodedName = $.UMUtilities.EncodeText(groupName);
            var encodedOwner = $.UMUtilities.EncodeText(groupOwner);

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: 'AddGroup',
                    groupName: encodedName,
                    description: descr,
                    ownerIdentifier: encodedOwner,
                    ownerType: oType,
                    defaultUserLoginName: encodedOwner,
                    async: false,
                    completefunc: function (xData, Status) {
                        //alert(Status);
                    }
                });
            }
            catch (e) {
                alert(e);
            }
        },

        AddGroupToRole: function (groupName, roleName, siteUrl)
        {
            var encodedName = $.UMUtilities.EncodeText(groupName);

            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: 'AddGroupToRole',
                    groupName: encodedName,
                    roleName: roleName,
                    async: false,
                    completefunc: function (xData, Status) {
                        //alert(Status);
                    }
                });
            }
            catch (e) {
                alert(e);
            }
        },

        GetRoleCollectionFromWeb: function (siteUrl) {

            var arr = [];
            if (siteUrl != '') {
                siteUrl = siteUrl;
            }
            else {
                siteUrl = $().SPServices.SPGetCurrentSite();
            }

            try {
                $().SPServices({
                    webURL: siteUrl,
                    operation: 'GetRoleCollectionFromWeb',
                    async: false,
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("Role").each(function () {
                            var item = { RoleName: $.UMUtilities.EncodeText($(this).attr("Name")), Description: $(this).attr("Description"), RoleID: $(this).attr("ID") };
                            arr.push(item);
                        });
                    }
                });

                return arr;
            }
            catch (e) {
                alert(e);
            }
        },
        /*
        GetUniqueGroupsFromAllSites: function () {

            var arryGroups = [];

            for (var i = 0; i < asites.length; i++) {
                try {
                    $().SPServices({
                        webURL: asites[i].Url,
                        operation: "GetGroupCollectionFromWeb",
                        async: false,
                        completefunc: function (xData, Status) {
                            $(xData.responseXML).find("Group").each(function () {
                                var encodedName = $.UMUtilities.EncodeText($(this).attr("Name"));
                                var item = { GroupName: encodedName, Description: $(this).attr("Description") };
                                arryGroups.push(item);
                            });
                        }
                    });
                }
                catch (e) {
                    alert(e);
                }
            }

            // Get Unique Groups Across All Site Collections
            var arryUniqueGroups = arryGroups.sort($.UserManagementSiteCollGroups.SortByName);
            var arrySiteCollGrps = [];
            var removeGrp = true;
            var cnt = 0;

            for (var i = 0; i < arryUniqueGroups.length; i++) {
                for (var j = i; j < arryUniqueGroups.length; j++) {
                    if (arryUniqueGroups[j].GroupName == arryUniqueGroups[i].GroupName) {
                        cnt = cnt + 1;
                    }
                }

                if (cnt == asites.length) {
                    arrySiteCollGrps.push(arryUniqueGroups[i]);
                    i = i + asites.length;
                }

                cnt = 0;
            }

            var dt = $('#siteCollGroupListTable').DataTable();
            dt.clear().rows.add(arrySiteCollGrps).draw();
        }
        */
    }
});